package alarm

import (
	"github.com/wuzfei/go-helper/time"
	"onvif_server/internal/model"
	"onvif_server/internal/repository"
	"onvif_server/internal/utils"
	"onvif_server/pkg/db"
)

type DetailRes struct {
	*model.Alarm
	Location string `json:"location"`
}

type ListReq struct {
	Status    int      `json:"status" query:"status" form:"status" binding:"omitempty"`
	EventType int      `json:"event_type" query:"event_type" form:"event_type" binding:"omitempty"`
	DeviceId  []string `json:"device_id" query:"device_id" form:"device_id" binding:"omitempty,unique"`
	StartTime string   `json:"start_time" query:"start_time" form:"start_time" binding:"omitempty"`
	EndTime   string   `json:"end_time" query:"end_time" form:"end_time" binding:"omitempty"`
	db.Paginator
}

func (r *ListReq) ToQueryFilter() (*repository.AlarmQueryFilter, error) {
	qf := repository.AlarmQueryFilter{
		Status:    r.Status,
		EventType: r.EventType,
		DeviceId:  r.DeviceId,
	}
	if r.StartTime != "" && r.EndTime != "" {
		dr, err := time.NewRangeFromString(r.StartTime, r.EndTime, utils.FormatDatetime)
		if err != nil {
			return nil, err
		}
		qf.DateRange = &dr
	}
	return &qf, nil
}

type DeleteReq struct {
	Days int   `json:"days" query:"days" form:"days" binding:"required_without=IDs"`
	IDs  []int `json:"ids" query:"ids" form:"ids" binding:"required_without=Days,omitempty,min=1,unique,dive,gt=0"`
}

type CreateReq struct {
	TaskId string `json:"task_id" query:"task_id" form:"task_id" binding:"required"`
	Image  string `json:"image" query:"image" form:"image" binding:"omitempty"`
	Video  string `json:"video" query:"video" form:"video" binding:"omitempty"`
	Ext    string `json:"ext" query:"ext" form:"ext" binding:"omitempty"`
}

type ProcessReq struct {
	ID     int    `json:"id" binding:"omitempty"`
	Status int    `json:"status" binding:"required"`
	Remark string `json:"remark" binding:"omitempty"`
}

type Setting struct {
	OpenCleanup bool `json:"open_cleanup" query:"open_cleanup" form:"open_cleanup" binding:"required"`
	CleanupDays int  `json:"cleanup_days" query:"cleanup_days" form:"cleanup_days" binding:"required"`
}

type WsNotifyMsgRes struct {
	Total int64        `json:"total"`
	Items []*DetailRes `json:"items"`
}
